//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of Syrinx Community Server (SCS).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==

/**
* ArticleListDisplay
*/
(function ($) {
    var methods = {
        init: function (options) {

            return this.each(function () {

                var $this = $(this),
             data = $this.data('listDisp');

                // If the plugin hasn't been initialized yet
                if (!data) {

                    /*
                    Do more setup stuff here
                    */

                    $(this).data('listDisp', {
                        target: $this,
                        options: options,
                        setupPager: false
                    });

                }
				if($this.children().length == 0)
					methods.showPage.apply(this, [1]);
            });
        },
        destroy: function () {

            return this.each(function () {

                var $this = $(this),
             data = $this.data('listDisp');

                $this.removeData('listDisp');

            })

        },
        showPage: function (pageNum) {
            var $this = $(this), data = $this.data('listDisp');
            var pagerId = "#" + $this.attr("id") + "-pager";
            data.options.page = pageNum;
            ksg.services.getArticles(data.options, function (json) {
                if (!data.setupPager) {
                    data.setupPager = true;
                    var numPages = (json.response.total / data.options.pageSize);
                    if (numPages > 1) {
                        var $pp = (typeof (data.options.pagerTemplateId) == "undefined") ? $("#" + $this.attr("id") + "-pager-page") : $(data.options.pagerTemplateId);
                        for (var p = 0; p < numPages; p++)
                            $pp.tmpl({ index: p + 1 }).appendTo(pagerId);

                        $(pagerId + " .page-selector").bind('click', function () {
                            methods.showPage.apply($this.get(0), [Number(this.id.substr(1))]);
                        });
                    }
                }
                data.totalRows = json.response.total;
                var $tmpl = (typeof (json.template) != "undefined") ? $(json.template) : null;
                if (data.options.templateId != null && typeof (data.options.templateId) != "undefined")
                    $tmpl = $(data.options.templateId);
                var resourceUrl = json.resourceUrl;
                if (data.options.resourceUrl != null && typeof (data.options.resourceUrl) != "undefined")
                    resourceUrl = data.options.resourceUrl;

                $(pagerId + " li.page-selector").removeClass("active");
                $(pagerId + " #i" + pageNum + ".page-selector").addClass("active");
                $this.children().remove();
                $tmpl.tmpl(json.response.bizObjs, $.extend({ resourceUrl: json.response.resourceUrl }, ksg.bizObjs.Article)).appendTo($this);
                $this.trigger("tmplApplied");
            },
        function (e) {
            var x = e;
        });
        }
    };

    $.fn.articleListDisplay = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.articleListDisplay');
        }

    };


})(jQuery);

